package _interview75;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 841. 钥匙和房间
 */
public class No841 {
    private boolean ans;
    private Set<Integer> path;
    private List<List<Integer>> rooms;

    public boolean canVisitAllRooms(List<List<Integer>> rooms) {
        ans = false;
        path = new HashSet<>();
        this.rooms = rooms;
        dfs(0);

        return ans;
    }

    private void dfs(int i) {
        path.add(i);
        if (path.size() == rooms.size()) ans = true;
        for (Integer j : rooms.get(i)) {
            if (!path.contains(j)) dfs(j);
        }
    }
}
